2024.7.1 solve_ivpのデータ評価点自動設定2【scipy】
solve_ivpに引数t_evalを与えることでデータ点を評価する時刻を設定できる。
code:ivp1.py
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
import numpy as np
def func(t, x):
global tu_hist
global t_hist
if t < 5:
u = 0
else:
u = 1
tu_hist.append(t)
u_hist.append(u)
return -x + u
ts = 0
tf = 10
u_hist, tu_hist = [], []
t_eval = np.linspace(ts, tf, 100)
sol = solve_ivp(func, trange, x_init, t_eval=t_eval)
t_hist = sol.t
fig = plt.figure()
ax = fig.add_subplot(2,1,1)
ax.set_title('x')
ax.grid()
ax.plot(t_hist, x_hist, '*-')
ax = fig.add_subplot(2,1,2)
ax.set_title('u')
ax.grid()
ax.plot(tu_hist, u_hist, '*-')
plt.show()
https://scrapbox.io/files/668219a0b19fbf001d56ba6d.png
状態の時間間隔は指定通り等間隔になったが、u の間隔はあいかわらずである。
https://scrapbox.io/files/668219f6ef4a4c001d1c3d9e.png
scipy.solve_ivp は、計算精度を向上させるために複雑な処理を行っていることが確認できた。